package com.youtellv1.controller;

import com.youtellv1.config.ResultData;
import com.youtellv1.dto.GSalDTO;
import com.youtellv1.service.GSalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/manager/gsal")
public class GSalController {
    @Autowired
    private GSalService gsalService;

    @GetMapping("/list")
    public ResultData list(@RequestParam Integer tid,
                           @RequestParam(defaultValue = "1") int page,
                           @RequestParam(defaultValue = "8") int size) {

        int total = gsalService.countByTid(tid);
        List<GSalDTO> list = gsalService.findByTidWithPage(tid, page, size);

        Map<String, Object> result = new HashMap<>();
        result.put("items", list);
        result.put("total", total);
        result.put("page", page);
        result.put("size", size);
        result.put("pages", (int) Math.ceil((double) total / size));

        return new ResultData<>(result);
    }

    @PostMapping("/add")
    @ResponseBody
    public ResultData addTeacherSalary(
            @RequestParam("base_sal") Float base_sal,
            @RequestParam("subsidy") Float subsidy,
            @RequestParam("edu") Float edu,
            @RequestParam("full_time") Float full_time,
            @RequestParam("phone") Float phone,
            @RequestParam("health") Float health,
            @RequestParam("safe") Float safe,
            @RequestParam("class_hour") Float class_hour,
            @RequestParam("parent") Float parent,
            @RequestParam("num") Float num,
            @RequestParam("year") Float year,
            @RequestParam("comm") Float comm,
            @RequestParam("post_sal") Float post_sal,
            @RequestParam("month") String month,
            @RequestParam("tid") Integer tid
    ) {
        GSalDTO dto = new GSalDTO();
        dto.setBase_sal(base_sal);
        dto.setSubsidy(subsidy);
        dto.setEdu(edu);
        dto.setFull_time(full_time);
        dto.setPhone(phone);
        dto.setHealth(health);
        dto.setSafe(safe);
        dto.setClass_hour(class_hour);
        dto.setParent(parent);
        dto.setNum(num);
        dto.setYear(year);
        dto.setComm(comm);
        dto.setPost_sal(post_sal);
        dto.setMonth(month);
        dto.setTid(tid);

        gsalService.insert(dto);
        return new ResultData<>();
    }

    @PostMapping("/update")
    @ResponseBody
    public ResultData updateTeacherSalary(
            @RequestParam("base_sal") Float base_sal,
            @RequestParam("subsidy") Float subsidy,
            @RequestParam("edu") Float edu,
            @RequestParam("full_time") Float full_time,
            @RequestParam("phone") Float phone,
            @RequestParam("health") Float health,
            @RequestParam("safe") Float safe,
            @RequestParam("class_hour") Float class_hour,
            @RequestParam("parent") Float parent,
            @RequestParam("num") Float num,
            @RequestParam("year") Float year,
            @RequestParam("comm") Float comm,
            @RequestParam("post_sal") Float post_sal,
            @RequestParam("month") String month,
            @RequestParam("tid") Integer tid,
            @RequestParam("sid") Integer sid
    ) {
        GSalDTO dto = new GSalDTO();
        dto.setBase_sal(base_sal);
        dto.setSubsidy(subsidy);
        dto.setEdu(edu);
        dto.setFull_time(full_time);
        dto.setPhone(phone);
        dto.setHealth(health);
        dto.setSafe(safe);
        dto.setClass_hour(class_hour);
        dto.setParent(parent);
        dto.setNum(num);
        dto.setYear(year);
        dto.setComm(comm);
        dto.setPost_sal(post_sal);
        dto.setMonth(month);
        dto.setTid(tid);
        dto.setSid(sid);

        gsalService.updateService(dto);
        return new ResultData<>();
    }

    @GetMapping("/delete/{sid}")
    public ResultData<Void> deleteSalaryByGet(@PathVariable Integer sid) {
        try {
            gsalService.deleteService(sid);
            return new ResultData<>(200, "删除成功", null);
        } catch (Exception e) {
            return new ResultData<>(500, "删除失败：" + e.getMessage(), null);
        }
    }

}
